# coding=utf-8
import ast
import json
import urllib
from urllib import parse
import requests


def translate(txt, dst_lang):
    url = 'https://translate.google.de/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&f.sid=3993856452033181795&bl=boq_translate-webserver_20210915.09_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=78964&rt=c'

    header = {
        'authority': 'translate.google.de'
        , 'method': 'POST'
        , 'path': "/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&f.sid=1280125120411349806&bl=boq_translate-webserver_20210915.09_p0&hl=zh-CN&soc-app=1&soc-platform=1&soc-device=1&_reqid=1167933&rt=c"
        , 'scheme': 'https'
        , 'accept': "*/*"
        , 'accept-encoding': 'gzip, deflate, br'
        , 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8'
        , 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
        ,
        'cookie': 'CONSENT=YES+DE.zh-CN+V15+BX; _ga=GA1.3.822873913.1613822873; 1P_JAR=2021-09-18-09; '
                  '_gid=GA1.3.130531127.1631983933; OTZ=6161332_48_52_123900_48_436380; '
                  'NID=223=o4POuSX_AmZYuqlvQ2fEmgdx1MlaPfrcm6dgMKjgqFpE1L6MuaapJ8rOSpo6Qfaik_tmGVETh'
                  'Y6ja7dyyX61DCKXPkci4Gkp_OhMVXVyUzolLXDJbptcV-apm8zRv-y8a8svt8a29tjLSElNhAFqU126J'
                  'q0zQYT3MXjk6N5x_C4'
        , 'origin': 'https://translate.google.de'
        , 'referer': 'https://translate.google.de/'
        , 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                        'Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3368.400 QQBrowser/9.6.11974.400'
        , 'x-same-domain': '1'
    }

    # data = 'f.req=%5B%5B%5B%22MkEWBc%22%2C%22%5B%5B%5C%22{0}%5C%22%2C%5C%22{1}%5C%22%2C%5C%22{2}%5C%22%2Ctrue%5D%2C%5Bnull%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D'
    # data = str.format(data, urllib.parse.quote(str(txt)), 'en', dst_lang)
    # req = requests.post(url=url, data=data, headers=header)  # 发送请求 , headers=header
    # print(req)
    # print(req.text)
    # l = req.text.split('\n')
    # print("********")
    # print(l[3])

    k = """[["wrb.fr","MkEWBc","[[null,null,null,[[[0,[[[null,31]],[true]]],[1,[[[null,32],[32,90]],[false,true]]],[2,[[[null,91],[91,110]],[false,true]]]],110]],[[[null,\"MongoDB ānzhuāng jiàochéng; jiāng shèqū bǎn shēngjí dào qǐyè bǎn jiàochéng; zhīchí de píngtái\",null,null,null,[[\"MongoDB安装教程;\",null,null,null,[[\"MongoDB安装教程;\",[5]],[\"MongoDB的安装教程;\",[1]]]],[\"将社区版升级到企业版教程;\",null,true,null,[[\"将社区版升级到企业版教程;\",[5]],[\"升级社区版到企业版教程;\",[1]]]],[\"支持的平台\",null,true,null,[[\"支持的平台\",[5,1]]]]]]],\"zh_CN\",1,\"en\",[\"MongoDB Installation Tutorials; Upgrade Community Edition to Enterprise Edition Tutorials; Supported Platforms\",\"en\",\"zh_CN\",true]],\"en\"]",null,null,null,"generic"],["di",32],["af.httprm",31,"2570112758651971253",22]]"""
    s = '["1","2","3","4"]'
    s2 = """[["wrb.fr","MkEWBc","[[null,null,null,[[[0,[[[null,31]],[true]]],[1,[[[null,32],[32,90]],[false,true]]],[2,[[[null,91],[91,110]],[false,true]]]],110]],[[[null,mongoDB ānzhuāng jiàochéng; jiāng shèqū bǎn shēngjí dào qǐyè bǎn jiàochéng; zhīchí de píngtái,null,null,null,[[MongoDB安装教程;,null,null,null,[[MongoDB安装教程;,[5]],[MongoDB的安装教程;,[1]]]],[将社区版升级到企业版教程;,null,true,null,[[\"将社区版升级到企业版教程;\",[5]],[\"升级社区版到企业版教程;\",[1]]]],[\"支持的平台\",null,true,null,[[\"支持的平台\",[5,1]]]]]]],\"zh_CN\",1,\"en\",[\"MongoDB Installation Tutorials; Upgrade Community Edition to Enterprise Edition Tutorials; Supported Platforms\",\"en\",\"zh_CN\",true]],\"en\"]",'null','null','null',"generic"],["di",29],["httprm",28,"-1460582250888529545",19]]"""
    # t = '@#$%^&*()'
    # s2 = s2.replace('\\"', t)
    # print(s2)
    # s2 = s2.replace('"', "'")
    # print(s2)
    # s2 = s2.replace(t,"\"")
    # print(str(s2))
    #print(ast.literal_eval(str(s2)))
    print(json.loads(s2))


s = 'MongoDB Installation Tutorials; Upgrade Community Edition to Enterprise Edition Tutorials; Supported Platforms'
translate(s, 'zh_CN')
